package com.smartalarmclock.UI;

import java.sql.Time;

import com.smartalarmclock.Logic.AcManager;
import com.smartalarmclock.Model.IProfile;
import com.smartalarmclock.Model.ITimeFrame;
import com.smartalarmclock.Model.TimeFrameFactory;
import com.smartalarmclock.Model.Exceptions.DataEntityDoesNotExistException;
import com.smartalarmclock.Model.ITimeFrame.DayInWeek;
import com.smartalarmclock.Model.ITimeFrame.TimeInWeek;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class CreateNewScheduleActivity extends Activity{
	
	private static final String TAG = "CreateNewScheduleActivity";
	private IProfile m_Profile = null;
	private ITimeFrame m_TimeFrame = null;
	private boolean m_IsNew = true;
    
	EditText m_EditTextBeginHour = null;
    EditText m_EditTextBeginMinute = null;
    EditText m_EditTextEndHour = null;
    EditText m_EditTextEndMinute = null;
    EditText m_EditTextDay = null;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.schedule_range_item);

    	    m_EditTextBeginHour = (EditText)findViewById(R.id.editText_Begin_Hour);
	    m_EditTextBeginMinute = (EditText)findViewById(R.id.editText_Begin_Minute);
	    m_EditTextEndHour = (EditText)findViewById(R.id.editText_End_Hour);
	    m_EditTextEndMinute = (EditText)findViewById(R.id.editText_End_Minute);
	    m_EditTextDay = (EditText)findViewById(R.id.editText_day);
	    
	    ImageView buttonSave = (ImageView)findViewById(R.id.imageView_apply_schedule_changes);
	    ImageView buttonDelete = (ImageView)findViewById(R.id.imageView_delete_schedule_changes);
	 // read extra messages sent to this activity intent
	    Bundle bundle = this.getIntent().getExtras();
	    int profileId = bundle.getInt(Messages.PROFILE_DETAILS_PROFILE_ID);
	    m_Profile = AcManager.GetInstance().GetProfileDetails(this, profileId);
	    m_IsNew = bundle.getBoolean(Messages.SCHEDULE_IS_NEW);
	    if (m_IsNew)
	    {
	    	buttonDelete.setVisibility(View.GONE);
	    }
	    else
	    {
	    	int timeFrameId = bundle.getInt(Messages.SCHEDULE_TIME_FRAME_ID);
	    	m_TimeFrame = AcManager.GetInstance().GetTimeFrameById(this, timeFrameId);
	    	if (m_TimeFrame == null)
	    	{
	    		try {
					throw new DataEntityDoesNotExistException(ITimeFrame.class);
				} catch (DataEntityDoesNotExistException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Log.v("IProfile", "Existing ITimeFrame in screen wasn't found in DB");
				}
	    	}
	    	
	    	setPreviouslyRecordedTime();
	    	buttonDelete.setOnClickListener(new OnClickDeleteListener(this));
	    }
	    
	    buttonSave.setOnClickListener(new OnClickSaveListener(this, m_Profile));
	}
	
	private void setPreviouslyRecordedTime()
	{	    
		if (m_TimeFrame == null)
		{
			Log.v(TAG, "m_TimeFrame is null! Bad!");
			throw new NullPointerException();
		}
		
		// Note: explicitly converting int to string because setText with 
		// Integer referees to an entry in the string table (and not the string representation of that Integer)
		Log.v(TAG, "In setPreviouslyRecordedTime, day value is: " + m_TimeFrame.GetEndTime().Day.GetValue());
		m_EditTextDay.setText(String.valueOf(m_TimeFrame.GetEndTime().Day.GetValue()));
		m_EditTextBeginHour.setText(String.valueOf(m_TimeFrame.GetStartTime().Time.getHours()));
		m_EditTextEndHour.setText(String.valueOf(m_TimeFrame.GetEndTime().Time.getHours()));
		m_EditTextBeginMinute.setText(String.valueOf(m_TimeFrame.GetStartTime().Time.getMinutes()));
		m_EditTextEndMinute.setText(String.valueOf(m_TimeFrame.GetEndTime().Time.getMinutes()));
		
	}
	private class OnClickSaveListener implements OnClickListener {

		private Context m_Context = null;
		private IProfile m_Profile = null;
		public OnClickSaveListener(Context context, IProfile i_Profile)
		{
			m_Context = context;
			m_Profile = i_Profile;
		}
		
		@Override
        public void onClick(View v)
        {
		    DayInWeek day = DayInWeek.FromValue(Integer.parseInt(m_EditTextDay.getText().toString()));
		    int beginHour = Integer.parseInt(m_EditTextBeginHour.getText().toString());
		    int beginMinute = Integer.parseInt(m_EditTextBeginMinute.getText().toString());
		    
		    int endHour = Integer.parseInt(m_EditTextEndHour.getText().toString());
		    int endMinute = Integer.parseInt(m_EditTextEndMinute.getText().toString());
		    
		    Time beginTime = new Time(beginHour, beginMinute, 0);
		    Time endTime = new Time(endHour, endMinute, 0);
		    
		    TimeInWeek beginTimeComplete = new TimeInWeek(day, beginTime);
		    TimeInWeek endTimeComplete = new TimeInWeek(day, endTime);
		    
		    TimeFrameFactory timeFrameFactory = new TimeFrameFactory();
		    ITimeFrame timeFrame = timeFrameFactory.CreateTimeFrame(beginTimeComplete, endTimeComplete);
		    
		    if (!m_IsNew)
		    {
		    	if (m_TimeFrame == null)
		    	{
		    		throw new NullPointerException();
		    	}
				AcManager.GetInstance().DeleteTimeFrame(m_Context, m_Profile, m_TimeFrame);
		    }
		    
	    	m_Profile.AddTimeFrame(m_Context, timeFrame);

	    	m_TimeFrame = timeFrame;
		    AcManager.GetInstance().ModifyProfile(m_Context, m_Profile);
			
		    finish();
		}
	};
	
	private class OnClickDeleteListener implements OnClickListener {

		private Context m_Context = null;
		public OnClickDeleteListener(Context context)
		{
			m_Context = context;
		}
		
		@Override
        public void onClick(View v)
        {
			Log.v(TAG, "Trying to delete Ofer");
			
			if (m_Profile != null && m_TimeFrame != null)
			{
				// delete the profile
				AcManager.GetInstance().DeleteTimeFrame(m_Context, m_Profile, m_TimeFrame);
			}
			
			// exit the activity
			finish();
		}
	};
}
